package com.study.mirrorforest.main.model.tags.service.impl;


import com.study.mirrorforest.main.common.response.R;
import com.study.mirrorforest.main.model.tags.dto.TagsDTO;
import com.study.mirrorforest.main.model.tags.mapper.TagsMapper;
import com.study.mirrorforest.main.model.tags.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TagsServiceImpl implements TagsService {

    @Autowired
    private TagsMapper tagsMapper;

    @Override
    public R CreateTag(String tagName) {
        // 检查标签是否已存在
        if (tagsMapper.selectByName(tagName) != null) {
            return R.error(400, "标签已存在");
        }

        // 创建标签
        TagsDTO tags = new TagsDTO();
        tags.setName(tagName);
        tags.setCreatedAt(new Date());
        tags.setUpdatedAt(new Date());
        tagsMapper.insert(tags);

        return R.ok(200, "创建标签成功");
    }

    @Override
    public R GetTagsList() {

        List<TagsDTO> tagsList = tagsMapper.selectAll();

        return R.ok(200, "获取标签列表成功", tagsList);
    }

    @Override
    public R DeleteTag(Integer id) {
        // 检查标签是否存在

        if (tagsMapper.selectByPrimaryKey(Long.valueOf(id)) == null) {
            return R.error(400, "标签不存在");
        }

        // 删除标签
        tagsMapper.deleteByPrimaryKey(Long.valueOf(id));


        return R.ok(200, "删除标签成功");
    }
}
